﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Movies.CopyUtility;
using System.Threading;

namespace Movies
{
    public partial class FormMovieFileAction : Form
    {
        public FormMovieFileAction()
        {
            InitializeComponent();
        }
        
        private List<String[]> sourceLocation;
        private int actionCommand = 0;

        public FormMovieFileAction(List<String[]> source,int action)
        {
            InitializeComponent();
            sourceLocation = source;
            actionCommand = action;
        }


        private void selectDestinationButton_Click(object sender, EventArgs e)
        {
            DialogResult result= folderBrowserDialog.ShowDialog();
            if(result == DialogResult.OK){
                String selectedPath = folderBrowserDialog.SelectedPath;
                if(selectedPath!=null && !selectedPath.Equals(""))
                {
                    destinationLocation.Text = selectedPath;
                    HelperSettingProvider.Default().FileActionLastSelectedPath = selectedPath;
                    HelperSettingProvider.Default().Save();
                }
            }
        }

        private void cancelCopy_Click(object sender, EventArgs e)
        {
                this.Dispose();
        }

        private void CopyAndMoveWin_Load(object sender, EventArgs e)
        {
            try
            {
                if (Directory.Exists(HelperSettingProvider.Default().FileActionLastSelectedPath))
                {
                    destinationLocation.Text = HelperSettingProvider.Default().FileActionLastSelectedPath;
                }
            }
            catch (Exception ex)
            {
                HelperContentProvider.OmitWarning(ex);
            }
            if (actionCommand == 1)
            {
                headerLabel.Text = "Moving following files:";
                startCopy.Text = "Start Moving";
                cancelCopy.Text = "Cancel Move";
                this.Text = "Move Files";
            }
            else if (actionCommand == 2)
            {
                headerLabel.Text = "Deleting following files:";
                startCopy.Text = "Start Deleting";
                this.Text = "Delete Files";
                cancelCopy.Text = "Cancel Delete";
                int additionalHeight = destinationPanel.Height;
                sourceFiles.Height = sourceFiles.Height + additionalHeight + 10;
                destinationPanel.Dispose();
            }
            foreach (String[] data in sourceLocation)
            {
                sourceFiles.Rows.Add(data);
            }
            startCopy.Focus();
        }

        private String destination = null;
        private void startCopy_Click(object sender, EventArgs e)
        {
            if (startCopy.Text.Equals("Done"))
            {
                this.Dispose();
                return;
            }
            else if (startCopy.Text.Equals("Run in Background"))
            {
                this.Hide();
                if (!isProcessStarted)
                {
                    HeplerBGProcesses.addBGProcess(this, copyProgress, "Coping Movies", false);
                    isProcessStarted = true;
                }

            }
            destination = destinationLocation.Text;
            if (startCopy.Text.Equals("Start Copy"))
            {

                Thread t = new Thread(delegate()
                {
                    initiateCopy();
                });
                t.Start();
                    
            }
            else if (startCopy.Text.Equals("Start Moving"))
            {
                Thread t = new Thread(delegate()
                {
                initiateMove();
                });
                t.Start();
            }
            else if (startCopy.Text.Equals("Start Deleting"))
            {
                Thread t = new Thread(delegate()
                {
                 initiateDelete();
                });
                t.Start();

            }
         }

        private void initiateCopy()
        {
            HelperFileDetailsGuide guide = new HelperFileDetailsGuide(sourceLocation);
            DTOFileMovementReport report = guide.setDestination(destination);
            if (!report.IsDestinationWriteAllowed)
            {
                WinDisplayControllor.DisplayDialog("Destination is not available for coping Movie(s)");
                return;
            }

            if (!report.IsSizeAvailbale)
            {
                WinDisplayControllor.DisplayDialog("Destination doesn't have sufficient available space");
                return;
            }

            updateForStart();
            guide.startCopy(copyProgress);
            updateForEnd();
        }

        private void initiateMove()
        {
            HelperFileDetailsGuide guide = new HelperFileDetailsGuide(sourceLocation);
            DTOFileMovementReport report = guide.setDestination(destination);
            if (!report.IsDestinationWriteAllowed)
            {
                WinDisplayControllor.DisplayDialog("Destination is not available for moving Movie(s)");
                return;
            }

            if (!report.IsSizeAvailbale)
            {
                WinDisplayControllor.DisplayDialog("Destination doesn't have sufficient available space");
                return;
            }

            updateForStart();
            guide.startMove(copyProgress);
            updateForEnd();
        }

        private void initiateDelete()
        {
            updateForStart();
            foreach (String[] data in sourceLocation)
            {
                if (Directory.Exists(data[1]))
                {
                    try
                    {
                        Directory.Delete(data[1],true);
                    }
                    catch (Exception ex)
                    {
                        HelperContentProvider.OmitWarning(ex);
                        WinDisplayControllor.DisplayDialog("Unable to Delete: " + data[1]);
                    }
                }
                else
                {
                    WinDisplayControllor.DisplayDialog("Doesn't Exists: " + data[1]);
                }
            }
            updateForEnd();

        }
        
        delegate void UpdateStart();
        delegate void UpdateEnd();


        private void updateForStart()
        {
            if (startCopy.InvokeRequired)
            {
                UpdateStart d = new UpdateStart(updateForStart);
                this.Invoke(d, new object[] { });
            }else{
                startCopy.Text = "Run in Background";
                cancelCopy.Hide();
                copyProgress.Show();
            }
        }

        private bool isProcessStarted; 

        private void updateForEnd()
        {
            if (startCopy.InvokeRequired)
            {
                UpdateEnd d = new UpdateEnd(updateForEnd);
                this.Invoke(d, new object[] { });
            }
            else
            {
                copyProgress.Value = copyProgress.Maximum;
                copyProgress.Hide();
                startCopy.Text = "Done";
            }
        }

    }
         
}
